package com.cws.project.cws.student.service.impl;

import com.cws.common.constant.UserConstants;
import com.cws.framework.shiro.service.PasswordService;
import com.cws.project.cws.clazz.domain.Clazz;
import com.cws.project.cws.clazz.domain.ClazzUser;
import com.cws.project.cws.clazz.mapper.ClazzUserMapper;
import com.cws.project.cws.student.domain.Student;
import com.cws.project.cws.student.mapper.StudentMapper;
import com.cws.project.cws.student.service.IStudentService;
import com.cws.project.system.user.domain.User;
import com.cws.project.system.user.domain.UserRole;
import com.cws.project.system.user.mapper.UserMapper;
import com.cws.project.system.user.mapper.UserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author XYL
 * @date 2021-12-22 10:27
 */
@Service
public class StudentServiceImpl implements IStudentService {
    private static Long STUDENT_ROLE_ID = 4L;

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PasswordService passwordService;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private ClazzUserMapper clazzUserMapper;

    @Override
    public List<Student> selectStudentList(Student stu) {
        return studentMapper.selectStudentList(stu);
    }

    @Override
    @Transactional
    public int insertStudent(Student stu) {
        //插入学生基本信息
        stu.setUserType(UserConstants.REGISTER_USER_TYPE);
        stu.randomSalt();
        stu.setPassword(passwordService.encryptPassword(stu.getLoginName(), stu.getPassword(), stu.getSalt()));
        int rows= userMapper.insertUser(stu);
        //插入角色
        insertUserRole(stu.getUserId(), STUDENT_ROLE_ID);
        //插入班级关联
        insertClazzUser(stu.getUserId(),stu.getClazzId());
        return rows;
    }

    @Override
    public Student selectUserById(Long userId) {
        return studentMapper.selectStudentByUserId(userId);
    }

    @Override
    @Transactional
    public int updateStudent(Student stu) {
        stu.setRoleId(STUDENT_ROLE_ID);
        int rows = userMapper.updateUser(stu);
        //解除班级关联
        clazzUserMapper.deleteClazzUserByUserId(stu.getUserId());
        //重新插入
        insertClazzUser(stu.getUserId(),stu.getClazzId());
        return rows;
    }


    /**
     * 新增用户角色信息
     */
    public void insertUserRole(Long userId, Long roleId) {
        List<UserRole> roles = new ArrayList<>();
        UserRole ur = new UserRole();
        ur.setUserId(userId);
        ur.setRoleId(roleId);
        roles.add(ur);
        userRoleMapper.batchUserRole(roles);
    }

    /**
     * 新增用户角色信息
     */
    public void insertClazzUser(Long userId, Long clazzId) {
        ClazzUser cu = new ClazzUser();
        cu.setUserId(userId);
        cu.setClazzId(clazzId);
        clazzUserMapper.insertClazzUser(cu);
    }
}
